﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using Microsoft.SystemCenter.VirtualMachineManager;
using System.Management.Automation; // Windows PowerShell namespace 
using System.Management.Automation.Runspaces; // Windows PowerShell namespac


namespace Erlacher.Opalis.VMMExtended
{
    class VMMHelper
    {
        public static List<TemplateInfo> GetTemplateInfoList(Collection<PSObject> templates)
        {
            if (templates == null)
                return null;
            else
            {
                List<TemplateInfo> list = new List<TemplateInfo>();
                foreach (PSObject t in templates)
                {
                    TemplateInfo info = GetTemplateInfo(t);
                    list.Add(info);
                }
                return list;
            }
        }

        public static TemplateInfo GetTemplateInfo(PSObject t)
        {
            if (t == null)
                return null;
            else
            {
                TemplateInfo info = new TemplateInfo();

                foreach (PSMemberInfo p in t.Members)
                {
                    try
                    {
                    switch (p.Name)
                        {


                            case "Admin":
                                info.Admin = p.Value.ToString();
                                break;
                            case "ComputerName":
                                info.ComputerName = p.Value.ToString();
                                break;
                            case "FullName":
                                info.FullName = p.Value.ToString();
                                break;
                            case "HostName":
                                info.HostName = p.Value.ToString();
                                break;
                            case "CreationTime":
                                info.CreationTime = DateTime.Parse(p.Value.ToString());
                                break;
                            case "ID":
                                info.Id = (Guid)p.Value;
                                break;
                            case "isHighlyAvailable":
                                info.isHighlyAvailable = bool.Parse(p.Value.ToString());
                                break;
                            case "LibraryGroup":
                                info.LibraryGroup = p.Value.ToString();
                                break;
                            case "Location":
                                info.Location = p.Value.ToString();
                                break;
                            case "Name":
                                info.Name = p.Value.ToString();
                                break;
                            case "OperatingSystem":
                                info.OperatingSystem = p.Value.ToString();
                                break;
                            case "Owner":
                                info.Owner = p.Value.ToString();
                                break;
                            case "Tag":
                                info.Tag = p.Value.ToString();
                                break;
                            case "TotalVHDCapacity":
                                info.TotalVHDCapacity = ulong.Parse(p.Value.ToString());
                                break;
                            case "VMHost":
                                if (p.Value != null)
                                    info.VMHost = p.Value.ToString();
                                break;
                            case "CustomProperties":

                                System.Collections.ArrayList mylist = (System.Collections.ArrayList)((PSObject)p.Value).BaseObject;

                                info.CustomProperty1 = mylist[0].ToString();
                                info.CustomProperty2 = mylist[1].ToString();
                                info.CustomProperty3 = mylist[2].ToString();
                                info.CustomProperty4 = mylist[3].ToString();
                                info.CustomProperty5 = mylist[4].ToString();
                                info.CustomProperty6 = mylist[5].ToString();
                                info.CustomProperty7 = mylist[6].ToString();
                                info.CustomProperty8 = mylist[7].ToString();
                                info.CustomProperty9 = mylist[8].ToString();
                                info.CustomProperty10 = mylist[9].ToString();


                                
                             
                        
                            break;
                        }
                    }
                    catch
                    {

                    }
                }

                return info;
            }
        }

    }
}
